import optparse
import os
import socket
import time
from threading import Thread
from io import StringIO

# 服务端程序处理过程
def server(listen_socket):
    while True:
        #buf = StringIO(txt)
        sock, addr = listen_socket.accept()
        print('Somebody at %s wants poetry!' %(addr,))
        while True:
            try:
                #help(sock)
                line = sock.recv(1024)
                if not line:
                    sock.close()
                    break
##                sock.sendall(line.encode('utf8')) # this is a blocking call
##                print('send bytes to client: %s' %line)
                print(line.decode())
                data  = line.decode()
                if data.find("S001") != -1:
                    sock.sendall(("R001,0;").encode(encoding="utf-8"))
                    
                if data.find("S002") != -1:
                    sock.sendall(("R002,0;").encode(encoding="utf-8"))
                    
                if data.find("S003") != -1:
                    sock.sendall(("R003,0;").encode(encoding="utf-8"))
                    
                if data.find("S004") != -1:
                    sock.sendall(("R004,0;").encode(encoding="utf-8"))
                    
                if data.find("S005") != -1:
                    sock.sendall(("R005,0;").encode(encoding="utf-8"))
                    
                if data.find("S006") != -1:
                    sock.sendall(("R006,0;").encode(encoding="utf-8"))
                    
                if data.find("S007") != -1:
                    sock.sendall(("R007,0;").encode(encoding="utf-8"))
                    
                if data.find("S008") != -1:
                    sock.sendall(("R008,0;").encode(encoding="utf-8"))
                    
                if data.find("S009") != -1:
                    sock.sendall(("R009,0;").encode(encoding="utf-8"))
                    
                if data.find("S010") != -1:
                    sock.sendall(("R010,0;").encode(encoding="utf-8"))
                    
                if data.find("S011") != -1:
                    sock.sendall(("R011,0;").encode(encoding="utf-8"))
                    
                if data.find("S012") != -1:
                    sock.sendall(("R012,0;").encode(encoding="utf-8"))
                    
                if data.find("S013") != -1:
                    sock.sendall(("R013,0;").encode(encoding="utf-8"))
                    
                if data.find("S014") != -1:
                    sock.sendall(("R014,0;").encode(encoding="utf-8"))
                    
                if data.find("S015") != -1:
                    sock.sendall(("R015,0;").encode(encoding="utf-8"))
                ##if data.find("RP@") != -1:
                ##sock.sendall((data).encode(encoding="utf-8"))

            except socket.error:
                sock.close()
                break
            time.sleep(0.5) # server每发送一个单词后等待一会
        sock.close()

# 同时开启三个服务端线程，分别在三个端口监听
# 服务端程序为阻塞方式，只能一次服务于一个客户端
def main():
    ports = [8080]
    for port in ports:
        listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        addres = ('127.0.0.1', port)
        listen_socket.bind(addres)
        listen_socket.listen(5)
        print("start listen at: %d" %port)
        worker = Thread(target = server, args = [listen_socket])
        worker.setDaemon(True)
        worker.start()

if __name__ == '__main__':
    main()
    while True:
        time.sleep(0.1) # 如果不sleep的话, CPU会被Python完全占用了
